<template>
  <div class="wrap_menu">
    <el-form ref="formData" :rules="rules" :model="formData" class="form-wrap" label-position="top" label-width="80px">
      <el-form-item label="会议室名称：" prop="DisplayName">
        <el-input v-model.trim="formData.DisplayName"></el-input>
      </el-form-item>
      <el-form-item label="会议室密码：">
        <el-input v-model.trim="formData.RoomPassword"></el-input>
      </el-form-item>
    </el-form>
    <div class="footer">
      <el-button class='slide-box__button' @click='cancelClick'>
        取消
      </el-button>
      <el-button class='slide-box__button' type="primary" @click='saveClick'>
        保存
      </el-button>
    </div>
  </div>
</template>
<script>
  import {deepClone} from 'src/utils/m7Utils.js'
  export default {
    name: 'roomDeal',
    data () {
      return {
        formData: { // 自定义满意度节点
          _id: '',
          DisplayName: '',
          Exten: '',
          RoomPassword: ''
        },
        rules: {
          DisplayName: [
            { required: true, message:this.$t('configLazyLoad.process.processLabel7'), trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      curPbx () {
        return this.$store.state.config.callCenter.currentPBX
      },
      Account () {
        return this.$store.state.session.user.account
      }
    },
    components: {
    },
    props: {
      form: {
        type: Object,
        default: function () {
          return {
            _id: '',
            DisplayName: '',
            Exten: '',
            RoomPassword: ''
          }
        }
      }
    },
    methods: {
      initForm (obj) {
        this.formData = deepClone(obj)
      },
      cancelClick () {
        this.$emit('cancelClick')
      },
      // 更新pbx数据
      updateVuexPbx () {
        this.$store.dispatch('queryRoom', {Account: this.Account, PBX: this.curPbx}).then(res => {
          if (res && res.success) {
            this.$store.commit('SET_IVRMAP_DETAIL', {PBX: this.curPbx, typeName: 'ivr_node_room', data: res.list || [], flag: 'data', node: this})
          }
          this.$emit('saveClick')
        })
      },
      saveClick () {
        this.$refs['formData'].validate((valid) => {
          if (valid) {
            let form = deepClone(this.formData)
            if (!form.id) {
              form.Account = this.Account
              form.PBX = this.curPbx
            }
            form.UpdateTime && delete form.UpdateTime
            this.$store.dispatch('updateRoom', form).then((res) => {
              if (res.success) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.updateVuexPbx()
              } else {
                this.$message.error('保存失败')
              }
            })
          }
        })
      }
    },
    beforeMount () {
      this.initForm(this.form)
    },
    watch: {
      form (cur, old) {
        this.$refs['formData'].resetFields()
        this.initForm(cur)
      }
    }
  }
</script>
<style lang="stylus" scoped>
  @import "../../../../../../assets/common.styl"
  .note
    margin-top 20px
    padding 0 20px
    p
      color $c-prompt
    p.two
      margin-top 10px
  .wrap_menu
    width 100%
    padding-bottom 60px
    .form-wrap
      padding 20px
  .footer
    position absolute
    bottom 0
    left 0
    width 100%
    background rgba(255,255,255,1)
    box-shadow 0px -2px 4px 0px rgba(5,49,38,0.05)
    border-top 1px solid rgba(232,232,232,1)
    display flex
    justify-content center
    padding 13px 0
    box-sizing border-box
    .slide-box__button
      padding 8px 20px 8px
      box-sizing border-box
</style>
